package view.game
{
	import business.GameDelegate;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.collections.XMLListCollection;
	import mx.containers.VBox;
	import mx.controls.Alert;
	import mx.controls.ComboBox;
	import mx.controls.LinkButton;
	import mx.events.CloseEvent;
	import mx.events.CollectionEvent;
	import mx.events.CollectionEventKind;
	import mx.events.ItemClickEvent;
	import mx.events.PropertyChangeEvent;
	import mx.rpc.Fault;
	import mx.rpc.events.FaultEvent;
	
	[Bindable]
	public class GameContentTypeClass extends VBox
	{
		public var contentTypesSrc:XML;
		public var contentTypes:XMLListCollection;
		
		public var cTypes:ArrayCollection;
		
		public var page:int;
		public var pagecnt:int;
		public var pageVolume:int;
		public var pageIndices:Array;
			
		public var cmEditorType:ComboBox;
		
		private var delegate:GameDelegate;

		private function onDataChanged(event:CollectionEvent):void
		{
			var item:Object;
			var updated:Object;
			var i:int;
			
			if(event.kind == CollectionEventKind.UPDATE)
			{
				for(i = 0; i <event.items.length; i++)
				{
					item = event.items[i];
					if ( item.property == 'selected' )
						continue;
					
					updated = new Object;
					updated.id = item.source.oldid;
					updated.property = item.property;
					updated.value = item.source[item.property];
					delegate.updateGameContentType(this.page,updated);
				}
			}
		}
		
		public function GameContentTypeClass()
		{
        	delegate = new GameDelegate(this);
        	
        	//delegate.getConfigs();
			this.page = 0;
			delegate.getGameContentType(this.page);			
			pageVolume = 20;
		}
		
		/*
		public function getConfigsResult(result:Object, token:Object = null):void
		{
			delegate.getGames(this.page);
			

		}
		*/
		
		public function goto(evt:MouseEvent):void
		{
			var page:int = LinkButton(evt.currentTarget).data.page;
			delegate.getGameContentType(page);
		}
		
		private function makePageIndices():void
		{
			var a:Array = new Array();
			a.push({page:0,desc:'首页'});			
			a.push({page:page-1,desc:'前一页'});			
			a.push({page:page+1,desc:'后一页'});
			a.push({page:pagecnt-1,desc:'尾页'});
			this.pageIndices = a;
		}             		
		
        public function clickHandler(event:ItemClickEvent):void 
        {
        	if ( event.index == 0 ) 	// add game
        	{
        		this.delegate.addGameContentType();	
        	}
        	else if ( event.index == 1 ) // select all
        	{
				for each ( var a:Object in this.cTypes )
				{
					a.selected = true;
				}

	            dispatchEvent(PropertyChangeEvent.createUpdateEvent
	            	(this,'cTypes',null,this.cTypes));
        	}
        	else if ( event.index == 2 ) // deselect all
        	{
				for each ( var a1:Object in this.cTypes )
				{
					a1.selected = false;
				}
				
	            dispatchEvent(PropertyChangeEvent.createUpdateEvent
	            	(this,'cTypes',null,this.cTypes));
        	}	
        	else if ( event.index == 3 ) // del selected
        	{
 				Alert.yesLabel = '确定';
				Alert.noLabel = '取消';
				Alert.buttonWidth = 50;
				Alert.buttonHeight = 18;
	
				Alert.show('您确定要删除吗?',
					'提示',Alert.YES | Alert.NO,null,onDelConfirmed,null,Alert.NO);
        	}
        }

        private function onDelConfirmed(event:CloseEvent):void 
        {
            if (event.detail==Alert.YES)
            {   	
       			var ids:Array = new Array;
        		
				for each ( var a:Object in this.cTypes )
				{
					if ( a.selected )
					{
						ids.push(a.id);
						a.selected = false;
					}
				}
				
				if ( ids.length )
				{
		            dispatchEvent(PropertyChangeEvent.createUpdateEvent
		            	(this,'cTypes',null,this.cTypes));
	        		
	        		this.delegate.removeGameContentType(this.page,ids);
	   			}	
            }	
        }
		        
		public function getGameContentTypeResult(result:Object, token:Object = null):void
		{
			this.refreshPage(result);
		}
				
		public function addGameContentTypeResult(result:Object, token:Object = null):void
		{
			this.refreshPage(result);
		}
						
		public function updateGameContentTypeResult(result:Object, token:Object = null):void
		{
			var ret:Object = result.result;
			
			if ( ret.code )
			{
				var fault:Fault = new Fault('',ret.msg);
				var error:FaultEvent = new FaultEvent('fault',false,true,fault);
				this.fault(error);
				return;
			}
		
			this.refreshPage(result);	
		}

		public function removeGameContentTypeResult(result:Object, token:Object = null):void
		{
			var ret:Object = result.result;
			
			if ( ret.code )
			{
				var fault:Fault = new Fault('',ret.msg);
				var error:FaultEvent = new FaultEvent('fault',false,true,fault);
				this.fault(error);
				return;
			}
			
			this.refreshPage(result);
		}
		
		public function fault(error:FaultEvent,token:Object = null):void
		{
			Alert.show(error.fault.faultString,'error');
		}
		
		private function refreshPage(result:Object):void
		{
			result = result.result;
			
			this.cTypes = new ArrayCollection(result.contentTypes);
			
			for each ( var a:Object in this.cTypes )
			{
				a.oldid = a.id
			}
			
			//trace(this.cTypes);
					
			this.page = result.page;
			this.pagecnt = result.pagecnt;

			this.makePageIndices();			
			
			this.cTypes.addEventListener(CollectionEvent.COLLECTION_CHANGE,onDataChanged);			
		}
	}	
}
